﻿using System;
using System.Data;
using System.Data.SqlClient;//SqlServer的驱动包

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            ///*
            //* 用户登录模拟
            //*/
            ////1.提示用户输入账号信息
            //Console.Write("请输入学号：");
            //String stuNo = Console.ReadLine();
            //Console.Write("请输入密码：");
            //String password = Console.ReadLine();

            ////2.查询用户信息（按学号和密码查询）
            //SqlConnection conn = new SqlConnection("Data Source=.;User ID=sa;pwd=as;Initial Catalog=StuManager;");
            //conn.Open();
            ////String sql = "SELECT * FROM Student WHERE StuNo='" + stuNo + "' AND Password='" + password + "'";
            //String sql = $"SELECT COUNT(*) FROM Student WHERE StuNo=@StuNo AND Password=@Password";
            //SqlCommand cmd = new SqlCommand(sql, conn);
            //cmd.Parameters.Add(new SqlParameter("@StuNo", SqlDbType.NVarChar));
            //cmd.Parameters.Add(new SqlParameter("@Password", SqlDbType.NVarChar));
            //cmd.Parameters[0].Value = stuNo;
            //cmd.Parameters[1].Value = password;

            //int cnt = Convert.ToInt32(cmd.ExecuteScalar());//返回查询的第一行第一列
            //conn.Close();

            ////3.判断是否登陆
            //if (cnt <= 0)
            //{
            //    Console.WriteLine("登陆失败");
            //}
            //else
            //{
            //    Console.WriteLine("登陆成功");
            //}

            /*
             * 添加操作
             */
            Console.Write("请输入姓名：");
            String name = Console.ReadLine();

            SqlConnection conn = new SqlConnection("Data Source=.;User ID=sa;pwd=as;Initial Catalog=StuManager;");
            conn.Open();

            String sql = $"INSERT INTO Student(StuNo,StuName,Gender,Birthday,Password) VALUES(@StuNo,@StuName,@Gender,@Birthday,@Password)";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(new SqlParameter("@StuNo", SqlDbType.NVarChar));
            cmd.Parameters.Add(new SqlParameter("@StuName", SqlDbType.NVarChar));
            cmd.Parameters.Add(new SqlParameter("@Gender", SqlDbType.NChar));
            cmd.Parameters.Add(new SqlParameter("@Birthday", SqlDbType.DateTime));
            cmd.Parameters.Add(new SqlParameter("@Password", SqlDbType.NVarChar));
            cmd.Parameters[0].Value = "000000000030";
            cmd.Parameters[1].Value = name;
            cmd.Parameters[2].Value = "男";
            cmd.Parameters[3].Value = "1998-8-8";
            cmd.Parameters[4].Value = "654321";
            int result = cmd.ExecuteNonQuery();
            Console.WriteLine(result);

            conn.Close();
        }
    }
}
